package com.kuang.controller;

import com.kuang.pojo.BOOKS;
import com.kuang.service.bookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
//    controller调service层
    @Autowired
    @Qualifier("bookServiceImpl")
    private bookService bookService;

//    查询全部的书籍，并且展示全部的书籍页面
    @RequestMapping("/allBook")
    public String querybook(Model model){
        List<BOOKS> booksList = bookService.queryAllbook();
        model.addAttribute("querybook",booksList);
        return "allBook";

    }

//    跳转到增添书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }

//    添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(BOOKS books){
        System.out.println("addBook=>"+books);
        bookService.addbook(books);
        return "redirect:/book/allBook"; //重定向到    @RequestMapping("/allBook")

    }

//跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdate(int id,Model model){
        BOOKS books = bookService.querybook(id);
        model.addAttribute("qbooks",books);
        return "update";
    }

//    修改书籍
    @RequestMapping("/updateBooks")
    public String updatebook(BOOKS books,Model model){
        System.out.println("updatebook=>"+books);
        bookService.updatebook(books);
//        BOOKS book = bookService.querybook(books.getBookID());
//        model.addAttribute("book",book);
        return "redirect:/book/allBook"; //重定向到    @RequestMapping("/allBook")

    }

//    删除书籍
    @RequestMapping("/deleteBook/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id){
        bookService.deletebook(id);
        return "redirect:/book/allBook"; //重定向到    @RequestMapping("/allBook")
    }

//    查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName,Model model){
        BOOKS books = bookService.queryBookByName(queryBookName);
        System.err.println("queryBook=>"+books);
//        List<BOOKS> list = new ArrayList<BOOKS>();
        List<BOOKS> list = new ArrayList<BOOKS>();
        list.add(books);
        if (books==null){
            list = bookService.queryAllbook();
            model.addAttribute("err","未查到");
        }
        model.addAttribute("list",list);
        return "allBook";


    }
}
